//
//  WCInputMessageVC.m
//  WC
//
//  Created by wsj2015 on 15/9/19.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCInputMessageVC.h"
#import "NSString+Utilities.h"
#import "Macros.h"
#import "WCDefaultManager.h"

#import "WCPopupLayerManager.h"
#import "WCNetworkAccessManager.h"
#import "WCButton.h"
#import "UINavigationItem+Offset.h"
#import "WCImageView +setImage.h"


@interface WCInputMessageVC ()<UITextFieldDelegate,UITextViewDelegate>
{
    NSDictionary *_user;
    NSInteger _btnSelecIndex;
    WCButton *_rightBtn;
   

}
@property (weak, nonatomic) IBOutlet UITextView *inputView;

@property (nonatomic, strong) WCButton *rightBtn;

@property (weak, nonatomic) IBOutlet UILabel *placeHoderLabel;
//@property (weak, nonatomic) IBOutlet UIButton *maleBtn;
//@property (weak, nonatomic) IBOutlet UIButton *femalBtn;
//@property (weak, nonatomic) IBOutlet UITextField *ageTF;
//@property (weak, nonatomic) IBOutlet UITextField *connectTF;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;

@property (weak, nonatomic) IBOutlet UILabel *messageContentLabel;

@end

@implementation WCInputMessageVC

- (WCButton *)rightBtn{
    if (!_rightBtn) {
        _rightBtn =
         _rightBtn= [WCButton wcButtonWithType:WCButtonTypeSave];
       
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
        [self.navigationItem addRightBarButtonItem:_rightBtn];
    }
    return _rightBtn;

}

- (void)keyboardShow:(NSNotification *) notif{
    
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
  
    [UIView animateWithDuration:.25f animations:^{
        
        CGRect frame = self.view.frame;
        frame.origin.y = 64.0;
        if (CGRectGetMaxY(frame) - CGRectGetMaxX(self.inputView.frame) < keyboardSize.height) {
            frame.origin.y = CGRectGetMaxY(frame) - CGRectGetMaxY(self.inputView.frame) - keyboardSize.height;
            self.view.frame = frame;
        }
    }];
    
}
- (void)keyboardHide:(NSNotification *) notif{
    
    [UIView animateWithDuration:.25f animations:^{
        CGRect frame =self.view.frame;
        frame.origin.y  =  64;
        self.view.frame =frame;
        
    }];
    
}

- (instancetype)init
{
    if (self = [super init]) {
//        _btnSelecIndex =0;
        
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyborarWillHide) name:UIKeyboardWillHideNotification object:nil];
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboradrWillShow) name:UIKeyboardWillShowNotification object:nil];
    }
    return self;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =kAppBg;
    self.title =@"回复留言";
    [self checkMessageIsReplyed];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //患者账号
   
}


- (void)configUIWithMessageModel:(WCNMessageContainer *)messageModel
{
    
    self.inputView.layer.borderWidth =1;
    self.inputView.layer.borderColor =kBorderColor.CGColor;
    self.inputView.layer.cornerRadius =20;
    self.inputView.layer.masksToBounds = YES;
    self.inputView.contentInset =UIEdgeInsetsMake(5, 0, 0, 0);
    
    [self.iconView setImageWithUrl:messageModel.avatarFileId];
    if (messageModel.nickName) {
        self.phoneLabel.text = [messageModel.nickName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }else{
        self.phoneLabel.text  =messageModel.mobile;
    
    }
   
    self.messageContentLabel.text = messageModel.messageContent;
    if (messageModel.replyContent) {
        self.inputView.text =messageModel.replyContent;
        self.inputView.editable = NO;
         [self.placeHoderLabel removeFromSuperview];
    }else{
        self.inputView.editable = YES;
         [self.rightBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
       
    
    }
    
   

 
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.inputView resignFirstResponder];
 
}



//控制输入字符数
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //回车键 结束
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }if (range.location>=100)
    {
        //控制输入文本的长度
        return  NO;
    }
    if ([text isEqualToString:@"\n"]) {
        //禁止输入换行
        return NO;
    }
    else
    {
        
        return YES;
    }
    
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    self.placeHoderLabel.hidden =YES;
//    CGRect rect = [textView convertRect:textView.bounds toView:self.view];
//    CGFloat y = self.view.frame.size.height - rect.origin.y - rect.size.height - 216;
//    [self moveView:y];
     return YES;
}
//- (void)moveView:(CGFloat)y
//{
//#warning 为了配合键盘弹出的动画，使用7<<16.
//    [UIView animateWithDuration:0.25
//                          delay:0.0
//                        options:7<<16
//                     animations:^{
//                         self.view.transform = CGAffineTransformMakeTranslation(0, y>=0?0:y-10);
//                     } completion:NULL];
//}

#pragma mark -查询该条的信息有没有回复 以及回复内容
- (void)checkMessageIsReplyed{
    
    [WCPopupLayerManager showLodingView];
    weakSelf_define;
    [[WCNetworkAccessManager sharedMg] POST:kSearchSingleMessage parameters:@{@"messageId":@(self.messageId)} success:^(NSDictionary *responseObject) {
        
        NSDictionary *dataDict =responseObject[@"data"];
        
        WCNMessageContainer *messageModel =[WCNMessageContainer objectWithKeyValues:dataDict];
        [weakSelf configUIWithMessageModel:messageModel];
        
    } failure:nil netError:nil always:^{
        [WCPopupLayerManager dissmissLodingView];
    }];
    


}

- (void)save
{
    if ([self.inputView.text isEqualToString: @""]) {
        [[WCPopupLayerManager shareMg] showTextOnly:@"意见内容不能为空" withView:self.view];
        return;
    }
    MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showWithLabel:@"正在提交....." withView:self.view];
    __weak typeof(self) weakSelf =self;
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"id"] =@(self.messageId);
    
    params[@"replyContent"] =self.inputView.text;
    [[WCNetworkAccessManager sharedMg]POST:kUpdataMessage parameters:params success:^(NSDictionary * responseObject) {
        BOOL isTure = [responseObject[@"success"] boolValue];
        if (isTure) {
            [[WCPopupLayerManager shareMg]showTextOnly:@"提交成功!!" withView:weakSelf.view];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        
    } failure:^(NSString *msg) {
        [[WCPopupLayerManager shareMg] showTextOnly:@"您的字数超过限制,请减少内容" withView:self.view];
        
        
    } netError:nil always:^{
        [hud hide:YES];
    }];

}


@end
